package com.faithbj.common.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Pattern;

import org.hibernate.annotations.GenericGenerator;

/**
 * @author jrn E-mail: rinanjin@gmail.com
 * @version 0.1 
 * 创建时间：2012-6-6 下午09:06:29
 * 类说明
 */
@Entity
@Table(name = "region")
public class Region implements Serializable
{
	private static final long serialVersionUID = 1L;
	
	// 市常量
	public static final Integer CITY = 1;
	
	// 区县常量
	public static final Integer COUNTRY = 2;
	
	// 街道常量
	public static final Integer STREET = 3;
	
	// 小区常量
	public static final Integer VILLAGE = 4;
	
	private String id;// ID
	
	// 名称
	private String name;

	// 编号
	@Pattern(regexp = "[\\d]{12}", message="编号必须为12位数字组成") 
	private String code;

	// 市、区县、街道、小区志位，1-市；2-区县；3-街道；4-小区
	private Integer regionType;
	
	// 上级行政区
	private Region parent;
	
	// 下级行政区
	private Set<Region> children;
	
	@Id
	@Column(length = 32)
	@GeneratedValue(generator = "uuid")
	@GenericGenerator(name = "uuid", strategy = "uuid")
	public String getId() {
		return id;
	}
	
	@Column(name = "name", nullable = false, unique = true)
	public String getName()
	{
		return name;
	}

	@Column(name = "code", nullable = false, unique = true)
	public String getCode()
	{
		return code;
	}
	
	@Column(name = "region_type", nullable = false)
	public Integer getRegionType()
	{
		return regionType;
	}

	@ManyToOne(fetch = FetchType.LAZY, cascade={CascadeType.REMOVE}, optional=true)
	@JoinColumn(name = "parent_id", nullable = true)
	public Region getParent()
	{
		return parent;
	}

	@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, orphanRemoval = true) 
	public Set<Region> getChildren()
	{
		return children;
	}

	public void setId(String id) {
		this.id = id;
	}

	
	public void setName(String name)
	{
		this.name = name;
	}

	public void setCode(String code)
	{
		this.code = code;
	}

	public void setRegionType(Integer regionType)
	{
		this.regionType = regionType;
	}

	public void setParent(Region parent)
	{
		this.parent = parent;
	}

	public void setChildren(Set<Region> children)
	{
		this.children = children;
	}

}
